package com.taiyi.renaissancemall.ui.main.personshare.personshare2;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ImageToFileUtils;
import com.taiyi.renaissancemall.Utils.RegexUtil;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.PersonShare2Bean;
import com.taiyi.renaissancemall.zxing.ZXingUtils;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;


import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

//分享商品
public class PersonShare2Activity extends BaseActivity<PersonShare2View, PersonShare2Present> implements PersonShare2View {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.tv_share_title)
    TextView tvShareTitle;
    @BindView(R.id.iv_share_icon)
    ImageView ivShareIcon;
    @BindView(R.id.iv_share_person_icon)
    ImageView ivSharePersonIcon;
    @BindView(R.id.iv_share_download)
    ImageView ivShareDownload;
    @BindView(R.id.iv_share_registered)
    ImageView ivShareRegistered;
    @BindView(R.id.previewIv)
    ImageView previewIv;
    @BindView(R.id.tv_share_person_name)
    TextView tvSharePersonName;
    @BindView(R.id.ll_share_choose)
    LinearLayout llShareChoose;
    @BindView(R.id.rl_share_bg2)
    RelativeLayout rlShareBg;
//    @BindView(R.id.rl)
//    RelativeLayout rlShareBg;
    @BindView(R.id.tv_share_code)
    TextView tvShareCode;
    @BindView(R.id.tv_share_price)
    TextView tvSharePrice;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.btn_toShare)
    Button btn_toShare;
    @BindView(R.id.tv_person_share_san)
    TextView tvPersonShareSan;
    private String cId = "";
    List<Integer> imgs = new ArrayList<>();
    private Bitmap shareBitmap;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person_share2;
    }

    @Override
    public PersonShare2Present initPresenter() {
        return new PersonShare2Present();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        cId = getIntent().getStringExtra("cId");
        titleTv.setText(getString(R.string.shareGoods));
        Collections.addAll(imgs, R.mipmap.good_list_one_share_one_icon, R.mipmap.good_list_one_share_two_icon
                , R.mipmap.good_list_one_share_three_icon, R.mipmap.good_list_one_share_four_icon
                , R.mipmap.good_list_one_share_five_icon);
        int position = (int) (0 + Math.random() * (4));
        rlShareBg.setBackgroundResource(imgs.get(position));


    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        presenter.toShareGoods(cId);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void shareGoodsSuccess(PersonShare2Bean personShare2Bean) {
        tvPersonShareSan.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        String sanPrice = getString(R.string.symbolMoney) + personShare2Bean.product.sanfprice;
        tvPersonShareSan.setText(sanPrice);
        String shopName = "";
        if (personShare2Bean.product.name.contains("\r\n")) {
            shopName = personShare2Bean.product.name.replace("\r\n", "");
        } else {
            shopName = personShare2Bean.product.name;
        }
        tvShareTitle.setText(shopName);

        RequestOptions options = new RequestOptions()
                .error(R.mipmap.person_share_avatar_iv)
                .disallowHardwareConfig()
                .placeholder(R.mipmap.person_share_avatar_iv)
                .circleCrop();
        RequestOptions options1 = new RequestOptions()
                .error(R.mipmap.person_share_avatar_iv)
                .disallowHardwareConfig()
                .placeholder(R.mipmap.person_share_avatar_iv)
                ;
        Glide.with(this).load(personShare2Bean.product.smallimgurl).apply(options1).into(ivShareIcon);
        Glide.with(this)
                .load(personShare2Bean.user.headimg)
                .apply(options)
                .into(ivSharePersonIcon);
        if (TextUtils.isEmpty(personShare2Bean.user.realname) || personShare2Bean.user.realname == null) {
            String hide_tel = RegexUtil.phoneNoHide(personShare2Bean.user.tel);
            tvSharePersonName.setText(hide_tel);
        } else {
            tvSharePersonName.setText(personShare2Bean.user.realname);
        }
        tvSharePrice.setText(personShare2Bean.product.price);
//        tvShareCode.setText(personShare2Bean.user.invitecode);
        Bitmap qrImage = ZXingUtils.createQRImage(personShare2Bean.androiddownurl, 100, 100);
        ivShareDownload.setImageBitmap(qrImage);
        Bitmap qrImage1 = ZXingUtils.createQRImage(personShare2Bean.sharecomurl, 100, 100);
        ivShareRegistered.setImageBitmap(qrImage1);
//        Glide.with(this).load(personShareBean.product.smallimgurl).into(ivShareDownload);
//        Glide.with(this).load(personShareBean.product.smallimgurl).into(ivShareRegistered);
    }

    @Override
    public void shareGoodsFailure(String msg) {
hideProgress();
    }

    @OnClick({R.id.backIv, R.id.iv_share_icon, R.id.btn_toShare})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.iv_share_icon:
                break;
            case R.id.btn_toShare:
                top.setVisibility(View.INVISIBLE);
                btn_toShare.setVisibility(View.INVISIBLE);
                try {
                    MyApp.getWxApi().registerApp(MyApp.APP_ID);
                    shareBitmap = ImageToFileUtils.captureView(rlShareBg);
                    shareBitmap=  shareBitmap.copy(Bitmap.Config.RGB_565,true);
//                    previewIv.setImageBitmap(shareBitmap);
                    //Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img);
                    //初始化 WXImageObject 和 WXMediaMessage 对象
                    WXImageObject imgObj = new WXImageObject(shareBitmap);
                    WXMediaMessage msg = new WXMediaMessage();
                    msg.mediaObject = imgObj;
                    //设置缩略图
                    Bitmap thumbBmp = Bitmap.createScaledBitmap(shareBitmap, 100, 100, true);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    thumbBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//                    shareBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                    byte[] data = baos.toByteArray();
                    thumbBmp.recycle();
//                    shareBitmap.recycle();
                    msg.thumbData = data;
                    //构造一个Req
                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    req.transaction = String.valueOf(System.currentTimeMillis());
                    req.message = msg;
                    req.scene = SendMessageToWX.Req.WXSceneSession;
                    // req.userOpenId = getOpenId();
                    //调用api接口，发送数据到微信
                    MyApp.getWxApi().sendReq(req);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                break;
        }
    }
}
